package com.calm.editor.handler;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Service;

import com.calm.editor.entity.Template;
import com.calm.editor.entity.TemplateKey;
import com.calm.editor.servlet.Result;

@Service("template")
public class TemplateService extends BaseService<Template> {

	public void getById(TemplateKey id, Result<Template> result) {
		result.setEvent("loadTemplate");
		Template byId = getById(id);
		result.setData(byId);
	}
	
	public void listByNamespace(final String id, Result<List<Template>> result) {
		result.setEvent("listByNamespace");
		List<Template> list=query(new Query<Template>() {

			@Override
			public void query(CriteriaBuilder builder,
					CriteriaQuery<Template> query, Root<Template> from) {
				Path<TemplateKey> path = from.get("id");
				builder.equal(path.get("namespace"), id);
			}
		});
		result.setData(list);
	}
	@Override
	public Class<Template> getEntityClass() {
		return Template.class;
	}
}
